export const getFunction = () => {
  // 计算点位一定距离的经纬度区间
  const calculateBounds = (lat, lon, radius) => {
    const R = 6371 // 地球半径，单位：公里
    const latRad = (lat * Math.PI) / 180
    const lonRad = (lon * Math.PI) / 180
    const radDist = radius / R
    const minLat = latRad - radDist
    const maxLat = latRad + radDist

    let minLon = 0,
      maxLon = 0
    if (minLat > -Math.PI / 2 && maxLat < Math.PI / 2) {
      const deltaLon = Math.asin(Math.sin(radDist) / Math.cos(latRad))
      minLon = lonRad - deltaLon
      maxLon = lonRad + deltaLon
    } else {
      // 包含极点的情况
      minLon = -Math.PI
      maxLon = Math.PI
    }

    return {
      minLat: (minLat * 180) / Math.PI,
      maxLat: (maxLat * 180) / Math.PI,
      minLon: (minLon * 180) / Math.PI,
      maxLon: (maxLon * 180) / Math.PI,
    }
  }
  const calculateDistance = (lat1, lon1, lat2, lon2) => {
    const R = 6371 // 地球半径，单位：公里
    const lat1Rad = (lat1 * Math.PI) / 180
    const lon1Rad = (lon1 * Math.PI) / 180
    const lat2Rad = (lat2 * Math.PI) / 180
    const lon2Rad = (lon2 * Math.PI) / 180

    const dLat = lat2Rad - lat1Rad
    const dLon = lon2Rad - lon1Rad

    const a =
      Math.sin(dLat / 2) * Math.sin(dLat / 2) +
      Math.cos(lat1Rad) *
        Math.cos(lat2Rad) *
        Math.sin(dLon / 2) *
        Math.sin(dLon / 2)

    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))

    return (R * c).toFixed(2) // 距离，单位：公里
  }
  const isPointInBounds = (point, bounds) => {
    // point: { lat, lon },
    // bounds: { minLat, maxLat, minLon, maxLon}
    return (
      point.lat >= bounds.minLat &&
      point.lat <= bounds.maxLat &&
      point.lon >= bounds.minLon &&
      point.lon <= bounds.maxLon
    )
  }

  return {
    calculateBounds,
    calculateDistance,
    isPointInBounds,
  }
}
